package com.nxw.springcloud.admin.controller;

import com.alibaba.fastjson.JSONObject;
import com.nxw.springcloud.admin.entity.AppKeyAndSecret;
import com.nxw.springcloud.admin.enums.EnvironmentEnum;
import com.nxw.springcloud.admin.service.inf.AppKeyAndSecretService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotEmpty;

/**
 * @program:
 * @description:
 * @author: nxw
 * @create: 2020-02-25 09:57
 **/
@RestController
@RequestMapping("/appKey")
public class AppKeyController {

    @Autowired
    AppKeyAndSecretService service;

    @GetMapping("/get")
    public AppKeyAndSecret getAppKey(EnvironmentEnum environment){
        return service.getAppKeyAndSecret(environment);
    }

    @GetMapping("/reset")
    public AppKeyAndSecret resetAppKey(String appKey,EnvironmentEnum environment){

        return StringUtils.isEmpty(appKey)?service.getAppKeyAndSecret(environment):service.resetAppKeyAndSecret(appKey,environment);
    }

    @GetMapping("/getAppKey")
    public String getAppKey(@Validated @NotEmpty String appKey,EnvironmentEnum environment){
        return service.getAppkeyByAppKey(appKey, environment)==null?"": JSONObject.toJSONString(service.getAppkeyByAppKey(appKey,environment));
    }


}
